class Register {
    form;
    ids;
    constructor() {
        this.form = document.querySelector("form");
        this.form.addEventListener("input", e => this.inputHandler(e));
        this.form.addEventListener("submit", e => this.submitHandler(e));
    }
    cleartime(input) {
        return new Promise((resolve, reject) => {
            this.ids = setTimeout(() => {
                clearTimeout(this.ids);
                this.ids = undefined;
                resolve(input.value)
            }, 500)
        })
    }
    async inputHandler(e) {
        if (this.ids) return;
        var value = await this.cleartime(e.target);
        console.log( e.target.name,value);
        if (this.testinput(e.target.name, value)) {
            e.target.nextElementSibling.style.display = 'none';
        } else {
           
            e.target.nextElementSibling.style.display = 'block';
        }
    }
    testinput(name, value) {
        switch (name) {
            case 'user':
                return /^1\d{10}$/.test(value);
            case 'password':
                return /^[A-Za-z0-9]{6,16}$/.test(value);
        }

    }
    async submitHandler(e) {
        e.preventDefault();
        var arr=[];
         new FormData(this.form).forEach((item,index)=>{
            arr.push(item)
        })
        var res = await fetch("http://localhost:4020/signUp", {
            method: "post",
            body: JSON.stringify(arr)
        });
        res= await res.json();
        res=res.result;
       if(!res){
           alert('账号已存在')
       }else{
      
           location.href="./login.html"+"?"+arr[0]
       }
    }
}
new Register()